from selenium import webdriver
from bs4 import BeautifulSoup
import dataset
import time

class App(object):
    db = None
    url = 'http://live.titan007.com/oldIndexall.aspx'
    driver = None

    def __init__(self):

        # 创建数据库链接 https://dataset.readthedocs.io/en/latest/api.html
        self.db = dataset.connect('sqlite:///data.db')

        # 驱动 https://www.selenium.dev/zh-cn/documentation/webdriver/getting_started/install_drivers/
        # self.driver = webdriver.Firefox()   # Firefox浏览器
        # self.driver = webdriver.Opera()     # Opera浏览器
        # self.driver = webdriver.Chrome()    # Chrome浏览器
        # self.driver = webdriver.Ie()        # Internet Explorer浏览器
        # self.driver = webdriver.PhantomJS('./driver/phantomjs.exe')   # PhantomJS
        self.driver = webdriver.Edge('./driver/msedgedriver.exe')  # Edge浏览器

    def run(self):
        # 打开url网页
        self.driver.get(self.url)

        # time.sleep(1)

        # 隐式等待
        self.driver.implicitly_wait(10)
        # 查找列表
        table = self.driver.find_element_by_xpath('/html/body/table').get_attribute('innerHTML')
        # 解析
        self.parserBs4(table)

        time.sleep(1)
        self.driver.quit()

    # 解析html
    def parserBs4(self, soup):
        # 获取列表
        bs4 = BeautifulSoup(soup, 'html.parser')
        trList = bs4.find('table', {'id': 'table_live'}).findAll('tr')
        # 遍历列表
        self.getList(trList)

    # 获取列表
    def getList(self, trList):

        # 遍历列表
        for i in range(1, len(trList)):
            item = trList[i]
            # 判断
            if len(item.findAll('td')) >3:
                self.getInfo(item.findAll('td'))

    def getInfo(self, row):

        tempDict = dict(
            title=row[1].get_text(),
            time=row[2].get_text(),
            status=row[3].get_text(),
            name_1=row[4].get_text(),
            score=row[5].get_text(),
            name_2=row[6].get_text(),
            half=row[7].get_text(),
            # data = 'row[8]',
            update_date=time.time()
        )


        # 插入数据库
        table = self.db["data"]

        res = table.find_one(
            title=row[1].get_text(),
            time=row[2].get_text(),
            name_1=row[4].get_text(),
            name_2=row[6].get_text()
        )

        # 判断
        if res is None:
           table.insert(tempDict)
        else:
            tempDict['id'] = res['id']
            table.update(tempDict, ['id'])

        print(tempDict)

if __name__ == "__main__":
    app = App()
    app.run()